package com.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.common.dto.PagingReqDTO;
import com.dao.QuMapper;
import com.dao.QuRepoMapper;
import com.entity.Qu;
import com.entity.QuRepo;
import com.entity.dto.QuRepoDTO;
import com.entity.dto.request.QuRepoBatchReqDTO;
import com.service.QuRepoService;
import com.service.RepoService;
import com.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 语言设置 服务实现类
 * </p>
 *
 * @author 聪明笨狗
 * @since 2020-05-25 13:23
 */
@Service
public class QuRepoServiceImpl extends ServiceImpl<QuRepoMapper, QuRepo> implements QuRepoService {


    @Autowired
    private QuMapper quMapper;

    @Autowired
    private RepoService repoService;

    @Override
    public PageUtils paging(PagingReqDTO<QuRepoDTO> reqDTO) {
        Page page = new Page();
        page.setCurrent(reqDTO.getCurrent());
        page.setSize(reqDTO.getSize());


        int pagingCount = baseMapper.pagingCount(reqDTO.getParams());
        page.setTotal(pagingCount);
        if (pagingCount > 0) {
            List<QuRepo> paging = baseMapper.paging(reqDTO.getParams());
            page.setRecords(paging);
        } else {
            page.setRecords(Collections.emptyList());
        }
        return new PageUtils(page);
    }

    @Override
    public void saveAll(String quId, Integer quType, List<String> ids) {
        // 先删除
        this.delete(new EntityWrapper<QuRepo>()
                .eq("qu_id", quId));

        // 保存全部
        if (!CollectionUtils.isEmpty(ids)) {
            List<QuRepo> list = new ArrayList<>();
            for (String id : ids) {
                QuRepo ref = new QuRepo();
                ref.setQuId(quId);
                ref.setRepoId(id);
                ref.setQuType(quType);
                list.add(ref);
            }
            this.insertBatch(list);


            for (String id : ids) {
                this.sortRepo(id);
            }
        }


    }

    @Override
    public List<String> listByQu(String quId) {
        // 先删除
        List<QuRepo> list = this.selectList(new EntityWrapper<QuRepo>()
                .eq("qu_id", quId));
        List<String> ids = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (QuRepo item : list) {
                ids.add(item.getRepoId());
            }
        }
        return ids;
    }

    @Override
    public List<String> listByRepo(String repoId, Integer quType, boolean rand) {
        EntityWrapper<QuRepo> wrapper = new EntityWrapper<QuRepo>();
        wrapper
                .eq("repo_id", repoId);

        if (quType != null) {
            wrapper.eq("qu_type", quType);
        }

        List<QuRepo> list = this.selectList(wrapper);
        List<String> ids = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (QuRepo item : list) {
                ids.add(item.getQuId());
            }
        }
        return ids;
    }

    @Override
    public void batchAction(QuRepoBatchReqDTO reqDTO) {

        // 移除的
        if (reqDTO.getRemove() != null && reqDTO.getRemove()) {
            EntityWrapper<QuRepo> wrapper = new EntityWrapper<QuRepo>();
            wrapper
                    .in("repo_id", reqDTO.getRepoIds())
                    .in("qu_id", reqDTO.getQuIds());
            this.delete(wrapper);
        } else {

            // 新增的
            for (String quId : reqDTO.getQuIds()) {
                Qu q = quMapper.selectById(quId);
                this.saveAll(quId, q.getQuType(), reqDTO.getRepoIds());
            }
        }

        for (String id : reqDTO.getRepoIds()) {
            this.sortRepo(id);
        }

    }


    /**
     * 单个题库进行排序
     *
     * @param repoId
     */
    private void sortRepo(String repoId) {

        EntityWrapper<QuRepo> wrapper = new EntityWrapper<QuRepo>();
        wrapper
                .eq("repo_id", repoId);

        List<QuRepo> list = this.selectList(wrapper);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        int sort = 1;
        for (QuRepo item : list) {
            item.setSort(sort);
            sort++;
        }
        this.updateBatchById(list);
    }
}
